//
//  HSYBaseWebModel.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/4.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYBaseWebModel.h"

@implementation HSYBaseWebModel

- (instancetype)initWithURL:(NSURL *)url {
    
    if (self = [super init]) {
        _url = url;
    }
    return self;
}


- (void)setHTTPCookiesForCompleted:(void(^)(void))completed {
    
    //定义cookie要设定的host
    NSURL *cookieHost = [NSURL URLWithString:BASE_URL];
    NSDictionary *cookies = @{
                              @"key"   :   @"cookies的相关请求头参数"
                              };
    
    [self rac_traverseDictionary:cookies nextBlock:^(NSString *key, NSString *value) {
        //设定cookie,根据需要设置
        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:@{
                                                                    NSHTTPCookieDomain : [cookieHost host],
                                                                    NSHTTPCookiePath : [cookieHost path],
                                                                    NSHTTPCookieName : key,
                                                                    NSHTTPCookieValue : value,
                                                                    }];
        //设定cookie到storage中
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    } completed:^{
        completed();
    }];
}

- (RACSignal *)rac_setHTTPCookies {
    
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        [self setHTTPCookiesForCompleted:^{
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}

@end

